package org.example.com.pattern.structure.bridge

/**
 * 结构型模式-桥接模式
 * 可以将一个大类或一系列紧密相关的类拆分为抽象和实现两个独立的层次结构，从而能在开发时分别使用
 *
 */

interface Device {
    fun isEnabled(): Boolean
    fun enable()
    fun disable()
    fun getVolume(): Int
    fun setVolume(volume: Int)
    fun getChannel(): Int
    fun setChannel(channel: Int)
}

class TV : Device {
    override fun isEnabled(): Boolean {
        return true
    }

    override fun enable() {
        println("TV enable")
    }

    override fun disable() {
        println("TV disable")
    }

    override fun getVolume(): Int {
        return 30
    }

    override fun setVolume(volume: Int) {
        println("TV setVolume $volume")
    }

    override fun getChannel(): Int {
        return 1
    }

    override fun setChannel(channel: Int) {
        println("TV setChannel $channel")
    }

}

class Radio : Device {
    override fun isEnabled(): Boolean {
        return true
    }

    override fun enable() {
        println("Radio enable")
    }

    override fun disable() {
        println("Radio disable")
    }

    override fun getVolume(): Int {
        return 10
    }

    override fun setVolume(volume: Int) {
        println("Radio setVolume")
    }

    override fun getChannel(): Int {
        return 2
    }

    override fun setChannel(channel: Int) {
        println("Radio setChannel $channel")
    }

}

open class RemoteControl(device: Device) {
    protected var device: Device = device
    fun togglePower() {
        if (device.isEnabled()) {
            device.enable()
        } else {
            device.disable()
        }
    }

    fun volumeDown() {
        device.setVolume(device.getVolume() - 10)
    }

    fun volumeUp() {
        device.setVolume(device.getVolume() + 10)
    }

    fun channelDown() {
        device.setChannel(device.getChannel() - 1)
    }

    fun channelUp() {
        device.setChannel(device.getChannel() + 1)
    }
}

class AdvancedRemoteControl(device: Device) : RemoteControl(device) {
    fun mute() {
        device.setVolume(0)
    }
}

fun main() {
    val tv = TV()
    var remote = RemoteControl(tv)
    remote.togglePower()

    val radio = Radio()
    remote = AdvancedRemoteControl(radio)
}